import {colorUISdk} from "../../config/mp-sdk";
import {updateCurrentPageShare} from "../../mp-sdk/lib/share";

Page({
    data: {
        status: 0,
        list: [],
        page: 1,
        limit: 10,
        loading: false,
        noMore: false,
        tab_val:0
    },

    onLoad(options) {
        this.setData({
            status: Number(options.status) || 0,
            tab_val : Number(options.status) || 0,
        });


        this.loadList(true);
    },

    loadList(isReset = false) {
        if (this.data.loading || this.data.noMore) return;

        if (isReset) {
            this.setData({
                page: 1,
                noMore: false,
                list: []
            });
        }

        this.setData({loading: true});

        colorUISdk.request({
            url: 'api/user/get_reward_list',
            method: 'GET',
            data: {
                status: this.data.status,
                page: this.data.page,
                limit: this.data.limit
            }
        }).then(res => {
            if (res.code === 1) {
                const resData = res.data;

                const newList = isReset ? resData.data : this.data.list.concat(resData.data);
                const noMore = resData.current_page >= resData.last_page;

                this.setData({
                    list: newList,
                    noMore: noMore,
                    page: resData.current_page + 1
                });
                // 登录成功后立即更新分享配置
                updateCurrentPageShare();

            } else {
                wx.showToast({title: res.msg, icon: 'none'});
            }
        }).finally(() => {
            this.setData({loading: false});
        });
    },
    tabChange(e){
        this.setData({status: e.detail.index,noMore: false,loading:false,list:[]});
        this.loadList(true);
    },


    onMerchantTransfer(e) {
        const package_info = e.currentTarget.dataset.package;
        const id = e.currentTarget.dataset.id;
        if (!package_info) return;
        //拉起商户转账
        this.pullMerchantTransfer(package_info, id);
    },

    pullMerchantTransfer(package_info, id) {
        if (wx.canIUse('requestMerchantTransfer')) {
            wx.requestMerchantTransfer({
                mchId: '1672563662',
                appId: wx.getAccountInfoSync().miniProgram.appId,
                package: package_info,
                success: (res) => {
                    //修改数据库状态
                    colorUISdk.request({
                        url: "api/user/confirm_reward",
                        method: "POST",
                        data: {id: id}
                    }).then(res => {
                        if (res.code === 1) {
                            const newList = this.data.list.filter(item => item.id !== package_info.id);
                            this.setData({
                                list: newList
                            });
                            wx.showToast({title: '提现成功', icon: "none"});
                        } else {
                            wx.showToast({title: res.msg, icon: "none"});
                        }

                        //改变列表中这一元素的状态
                        /* */
                    });
                },
                fail: (res) => {
                    console.log('用户取消提现')
                },
            });
        } else {
            wx.showModal({
                content: '你的微信版本过低，请更新至最新版本。',
                showCancel: false,
            });
        }
    },

    onReachBottom() {
        this.loadList(false);
    },

    onPullDownRefresh() {
        this.loadList(true);
    },
    withdrawOne(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: "确认提现",
            content: "确定将此笔奖励提现吗？",
            success: res => {
                if (res.confirm) {
                    colorUISdk.request({
                        url: "api/user/apply_withdraw",
                        method: "POST",
                        data: {id: id}
                    }).then(res => {
                            if (res.code === 1) {
                                const package_info =  res.data.withdraw_res.package_info;
                                //拉起商户转账
                                this.pullMerchantTransfer(package_info, id);
                            } else {
                                wx.showToast({title: res.msg, icon: "none"});
                            }
                        }
                    ).catch(() => {
                        wx.showToast({title: "网络错误", icon: "none"});
                    });
                }
            }
        });
    }

});
